<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item :label="t('student.fields.batchId')" prop="batchId">
        <el-select
          v-model="queryParams.batchId"
          :placeholder="t('common.selectTextPlaceholder') + t('student.fields.batchId')"
          clearable
          class="!w-160px"
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in batchOptions"
            :key="index"
            :label="item.batchName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('systemUser.fields.nickname')" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          :placeholder="t('common.inputTextPlaceholder') + t('systemUser.fields.nickname')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iDoubleChioceMentor-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column type="checkbox" width="50" align="center" fixed="left" />
      <vxe-column
        :title="t('systemUser.fields.username')"
        field="username"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.nickname')"
        field="mentorName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.jobCode')"
        field="mentorJobCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.mobile')"
        field="mentorPhone"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.email')"
        field="mentorEmail"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.identity')"
        field="mentorIdentity"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.sex')"
        field="mentorGender"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.postIds')"
        field="mentorTitle"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.deptName')"
        field="mentorDept"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('systemUser.fields.workingStatus')"
        field="mentorWorkStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <dict-tag
            v-if="scope.row.mentorWorkStatus"
            :type="DICT_TYPE.WORKING_STATUS"
            :value="scope.row.mentorWorkStatus"
          />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleViewChoice(scope.row)"
            v-hasPermi="['recruit:student:update']"
          >
            {{ t('doubleChioce.index.viewChoice') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <DoubleChoiceDetailMentor ref="mentorChoiceDetailRef" />
</template>

<script setup lang="ts">
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { BatchApi, BatchVO } from '@/api/recruit/batch'
import { BatchMentorApi, BatchMentorChoiceRespVO } from '@/api/recruit/batchmentor'
import { DICT_TYPE } from '@/utils/dict'
import DoubleChoiceDetailMentor from './DoubleChoiceDetailMentor.vue'

/** 双选 列表 */
defineOptions({ name: 'DoubleChoiceMentor' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<BatchMentorChoiceRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchId: undefined,
  nickname: undefined
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BatchMentorApi.getMentorChoicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const mentorChoiceDetailRef = ref<InstanceType<typeof DoubleChoiceDetailMentor>>()
// 双选详情弹窗
const handleViewChoice = (row: BatchMentorChoiceRespVO) => {
  mentorChoiceDetailRef.value?.open(row.userId)
}

const batchOptions = ref<BatchVO[]>([])
const getBatchSelectOptions = async () => {
  batchOptions.value = await BatchApi.getSelectList()
}

/** 初始化 **/
onMounted(async () => {
  // 先查询所有计划
  await getBatchSelectOptions()
  // 然后查询最新计划的数据
  queryParams.batchId = (batchOptions.value[0]?.id ?? undefined) as any

  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell .el-button + .el-button) {
  margin-left: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 15px !important;
}
</style>
